package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.MsgResult;
import com.itheima.common.R;
import com.itheima.dao.TbCourseDao;
import com.itheima.dto.CourseDto;
import com.itheima.pojo.TbCourse;
import com.itheima.service.TbCourseService;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (TbCourse)表服务实现类
 */
@Service("tbCourseService")
public class TbCourseServiceImpl implements TbCourseService {
    @Resource
    private TbCourseDao tbCourseDao;

    //课程分页条件查询接口
    @Override
    public R listCourse(CourseDto courseDto) {
        IPage<TbCourse> page = new Page<>(courseDto.getPageNum(),courseDto.getPageSize());
        LambdaQueryWrapper<TbCourse> wrapper = new LambdaQueryWrapper<>();
        if (courseDto.getCode()!=null){
            wrapper.like(TbCourse::getCode,courseDto.getCode());  //课程编号
        }
        if (courseDto.getName()!=null){
            wrapper.like(TbCourse::getName,courseDto.getName());   //课程名称
        }
        if (courseDto.getApplicablePerson()!=null){
            wrapper.eq(TbCourse::getApplicablePerson,courseDto.getApplicablePerson()); //适用人群
        }
        if (courseDto.getSubject()!=null){
            wrapper.eq(TbCourse::getSubject,courseDto.getSubject());  //课程学科
        }
        if (courseDto.getBeginCreateTime()!=null&&courseDto.getEndCreateTime()!=null){
            wrapper.between(TbCourse::getCreateTime,courseDto.getBeginCreateTime(),courseDto.getEndCreateTime());  //创建时间
        }
        wrapper.orderByDesc(TbCourse::getCreateTime);  //默认数据按时间倒序排列
        IPage<TbCourse> tbCourseIPage = tbCourseDao.selectPage(page, wrapper);
        return new R(1, MsgResult.SUCCESS_FIND,tbCourseIPage);
    }

    //添加课程接口
    @Override
    public R addCourse(TbCourse tbCourse) {
        tbCourse.setCreateTime(new Date());
        int insert = tbCourseDao.insert(tbCourse);
        if (insert>0){
            return new R(1,MsgResult.SUCCESS_ADD,"");
        }
        return new R(1,MsgResult.WRONG_ADD,"");
    }

    //修改课程--确认修改接口
    @Override
    public R modifyCourse(TbCourse tbCourse) {
        int i = tbCourseDao.updateById(tbCourse);
        if (i>0){
            return new R(1,MsgResult.SUCCESS_MODIFY,"");
        }
        return new R(0,MsgResult.WRONG_MODIFY,"");
    }

    //修改课程--课程回显接口
    @Override
    public R getCourseById(Long id) {
        TbCourse tbCourse = tbCourseDao.selectById(id);
        if (tbCourse==null){
            return new R(0,MsgResult.WRONG_FIND,"");
        }
        return new R(1,MsgResult.SUCCESS_FIND,tbCourse);
    }

    //删除课程接口
    @Override
    public R removeCourse(Long id) {
        int i = tbCourseDao.deleteById(id);
        if (i>0){
            return new R(1,MsgResult.SUCCESS_REMOVE,"");
        }
        return new R(0,MsgResult.WRONG_REMOVE,"");
    }

    //合同管理的高级搜索的购买课程列表选择
    @Override
    public R listSelect() {
        List<TbCourse> tbCourses = tbCourseDao.selectList(null);
        return new R(1,MsgResult.SUCCESS_FIND,tbCourses);
    }

    //批量删除课程接口
    @Override
    public R removeCoueses(Long[] ids) {
        for (Long id : ids) {
            tbCourseDao.deleteById(id);
        }
        return new R(1,MsgResult.SUCCESS_REMOVE,"");
    }

}
